package jdbc;

import java.sql.*;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class TestUpdate2 {
    public static void main(String[] args) {
        //Prepardstatement
        Connection con = DBUtil.getConnection();
        PreparedStatement st = null;
        //update subject set =xxx where id=
        Scanner in=new Scanner(System.in);
        System.out.println("请输入课程的名称，学时，年级和编号：");
        String newName=in.nextLine();
        //?newName=newName.replaceAll("'","\\'");

        int classhour=in.nextInt();
        long grade=in.nextLong();
        long id=in.nextLong();

        try {
            String sql="update s_subject set subjectname=?,classhour=?,gradeid=? where id=?";

            st=con.prepareStatement(sql);
            st.setString(1,newName);
            st.setInt(2,classhour);
            st.setLong(3,grade);
            st.setLong(4,id);

            int res = st.executeUpdate();//不需要Sql作为参数
            System.out.printf("成功影响了%d行",res);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(con,st,null);
        }
    }
}
